package com.bezdelnick.forum.web.component;

import com.bezdelnick.forum.domain.AppUser;
import com.bezdelnick.forum.service.ForumService;
import com.bezdelnick.forum.web.page.ShowUsersListPage;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 30.08.12
 * Time: 23:29
 * To change this template use File | Settings | File Templates.
 */
public class UsersListView extends ListView {
    private static final Logger LOGGER = LoggerFactory.getLogger(UsersListView.class);

    @SpringBean
    @SuppressWarnings("unused")
    private ForumService forumService;

    public UsersListView(String id, List list) {
        super(id, list);
        /* Add this instance to Spring context */
        SpringComponentInjector.get().inject(this);
    }

    @Override
    protected void populateItem(ListItem listItem) {
        try {
            final AppUser user;
            user = (AppUser) listItem.getModelObject();

            listItem.add(new Label("username", user.getUsername()));
            listItem.add(new Label("email", user.getEmail()));

            BookmarkablePageLink bplActionEnableOrDisable = new BookmarkablePageLink("action_1", ShowUsersListPage.class);
            if (user.isEnabled()) {
                bplActionEnableOrDisable.setBody(new Model<Serializable>("disable"));
                bplActionEnableOrDisable.getPageParameters().add("action", "disable");
            } else {
                bplActionEnableOrDisable.setBody(new Model<Serializable>("enable"));
                bplActionEnableOrDisable.getPageParameters().add("action", "enable");
            }
            bplActionEnableOrDisable.getPageParameters().add("id", user.getId());
            listItem.add(bplActionEnableOrDisable);

            BookmarkablePageLink bplActionRemove = new BookmarkablePageLink("action_2", ShowUsersListPage.class) {
                @Override
                public void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
                    replaceComponentTagBody(markupStream, openTag, "remove");
                }
            };
            bplActionRemove.getPageParameters().add("action", "remove");
            bplActionRemove.getPageParameters().add("id", user.getId());
            listItem.add(bplActionRemove);
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
        }
    }
}
